/*
   Copyright (c) 2011, 2012, 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *  Marco Riccardi
 *	2011-02-18 - initial implementation
 *  2012-04-06 - added message in DliParseException
 *  2015-02-07 - added FlexFrm
 *
 */
package org.mizartools.dli;

public class Formula {

	private String string = null;

	public static Formula newInstance(String string) throws DliParseException {
		int position = string.indexOf("(");
		if (position == -1) {
			if (string.equals("$VERUM")) {
				return Verum.newInstance(null);
			}
			if (string.startsWith("$private_formula ")){
				Formula privateFormula = PrivateFormula.newInstance(string);
				return privateFormula;
			}
			if (string.equals("$FlexFrm")) {
				return FlexFrm.newInstance(null);
			}
			int positionPred = string.indexOf(":pred ");
			if ( positionPred != -1){
				Pred pred = Pred.newInstance(string);
				return pred;
			}
			throw new DliParseException("["+string+"] is not valid in Formula");
		} else {
			String formulaType = string.substring(0, position);
			if (formulaType.equals("$for")){
				Formula for_ = For.newInstance(string.substring(position+1, string.length()-1));
				return for_;
			}
			if (formulaType.equals("$not")){
				Formula not = Not.newInstance(string.substring(position+1, string.length()-1));
				return not;
			}
			if (formulaType.equals("$and")){
				Formula and = And.newInstance(string.substring(position+1, string.length()-1));
				return and;
			}
			if (formulaType.startsWith("$private_formula ")){
				Formula privateFormula = PrivateFormula.newInstance(string);
				return privateFormula;
			}
			if (formulaType.equals("$is")){
				Formula privateFormula = Is.newInstance(string);
				return privateFormula;
			}
			if (formulaType.equals("$part")){
				Formula privateFormula = Part.newInstance(string);
				return privateFormula;
			}
			int positionPred = string.indexOf(":pred ");
			if ( positionPred!=-1 && positionPred < position){
				Pred pred = Pred.newInstance(string);
				return pred;
			}
			
			Formula formula = new Formula();
			formula.string = string;
			return formula;
		}
	}

	protected Formula(){}
	
	@Override
	public String toString() {
		return "[[Formula]"+string+"]";
	}

	public static boolean isFormula(String string) {
		if (string.equals("$VERUM")) {
			return true;
		} 
		if (string.startsWith("$for")){
			return true;
		} 
		if (string.startsWith("$not")){
			return true;
		}
		if (string.startsWith("$and")){
			return true;
		}
		if (string.startsWith("$private_formula ")){
			return true;
		}
		if (string.startsWith("$is")){
			return true;
		}
		if (string.startsWith("$part")){
			return true;
		}
		int positionPred = string.indexOf(":pred ");
		int position = string.indexOf("(");
		if ( positionPred!=-1 && positionPred < position){
			return true;
		}
		return false;
	}

}
